#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
进行配置的操作

 - writer:linux_23; create: Jun 27 22:23:47 2008 ; version:1; 创建
"""
from utils import TALK_CONF
import uuid

class GisData:
    def get_gisdivs(self):
        if 'GIS_DIVS' not in TALK_CONF:
            TALK_CONF['GIS_DIVS'] = "[]"
        divlists = TALK_CONF['GIS_DIVS']
        exec "divs = "+divlists
        return divs
    def get_datasets(self, divname):
        if 'GIS_DATASETS_'+divname not in TALK_CONF:
            TALK_CONF['GIS_DATASETS_'+divname] = "[]"
        divlists = TALK_CONF['GIS_DATASETS_'+divname]
        exec "datasets = "+divlists
        return datasets
    def get_mapids(self):
        if 'GIS_MAPS' not in TALK_CONF:
            TALK_CONF['GIS_MAPS'] = "[]"
        maplists = TALK_CONF['GIS_MAPS']
        exec "maps = "+maplists
        return maps
    def get_maps_names(self):
        mapids = self.get_mapids()
        names = []
        for mid in mapids:
            names.append(TALK_CONF[mid])
        return names

    def add_gisdiv(self,divname):
        divs = self.get_gisdivs()
        if divname in divs:
            return 
        divs.append(divname)
        TALK_CONF['GIS_DIVS'] = str(divs)
    def add_gisdataset(self,divname, dataset):
        divs = self.get_datasets(divname)
        if dataset in divs:
            return 
        divs.append(dataset)
        TALK_CONF['GIS_DATASETS_'+divname] = str(divs)

    def reg_map(self,mapname):
        mapnames = self.get_maps_names()
        if mapname in mapnames:
            return 
        mid = str(uuid.uuid1())
        TALK_CONF[mid] = mapname
        maps = self.get_mapids()
        maps.append(mid)
        TALK_CONF['GIS_MAPS'] = str(maps)

    def unreg_map(self, mapname):
        mapnames = self.get_maps_names()
        if mapname not in mapnames:
            return 
        maps = self.get_mapids()
        mid = None
        for mapid in maps:
            if TALK_CONF[mapid] == mapname:
                mid = mapid
                break

        maps.remove(mid)
        TALK_CONF['GIS_MAPS'] = str(maps)
        TALK_CONF.pop(mid)

    def get_reggisted_gisdata(self, divname):
        if "DIV_"+divname not in TALK_CONF:
            TALK_CONF["DIV_"+divname] = "[]"
        datalists = TALK_CONF['DIV_'+divname]
        exec "datas = "+datalists
        return datas

    def reg_gisdata(self, divname, name, datapath):
        datalist = self.get_reggisted_gisdata(divname)
        if name+":/:"+datapath in datalist:
            return
        datalist.append(name+ ":/:"+ datapath)
        TALK_CONF['DIV_'+divname] = str(datalist)

    def unreg_gisdata(self, divname, name):
        print divname
        datalist = self.get_reggisted_gisdata(divname)
        i = 0
        for data in datalist:
            if data.startswith(name+":/:"):
                datalist.pop(i)
                break;
            i+=1
        TALK_CONF['DIV_'+divname] = str(datalist)

    def get_mapid(self, mapname):
        exec "maps = "+TALK_CONF['GIS_MAPS']
        for k in maps:
            if TALK_CONF[k] == mapname:
                return k
        return ''

    def get_reggisted_mapdata(self, mapname):
        mid = self.get_mapid(mapname)
        if "MAPDATA_"+str(mid) not in TALK_CONF:
            TALK_CONF["MAPDATA_"+mid] = "[]"
        datalists = TALK_CONF['MAPDATA_'+mid]
        exec "datas = "+datalists
        return datas

    def reg_mapdata(self, mapname, name):
        mid = self.get_mapid(mapname)
        datalist = self.get_reggisted_mapdata(mapname)
        if name not in datalist:
            datalist.append(name)
        TALK_CONF['MAPDATA_'+mid] = str(datalist)

    def unreg_mapdata(self, mapname, name):
        mid = self.get_mapid(mapname)
        datalist = self.get_reggisted_mapdata(mapname)
        if name in datalist:
            datalist.remove(name)
        TALK_CONF['MAPDATA_'+mid] = str(datalist)

    def get_data(self, divpath):
        return divpath.split(":/:")

print TALK_CONF

GISDATA = GisData()
